{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c2e78c46",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from sklearn.metrics.pairwise import cosine_similarity"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "e990b3ba",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_csv('./data/book/BX-Book-Ratings.csv',sep=';',encoding='latin-1',usecols=['User-ID','ISBN','Book-Rating'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "2d167519",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>User-ID</th>\n",
       "      <th>ISBN</th>\n",
       "      <th>Book-Rating</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>276725</td>\n",
       "      <td>034545104X</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>276726</td>\n",
       "      <td>0155061224</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>276727</td>\n",
       "      <td>0446520802</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>276729</td>\n",
       "      <td>052165615X</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>276729</td>\n",
       "      <td>0521795028</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   User-ID        ISBN  Book-Rating\n",
       "0   276725  034545104X            0\n",
       "1   276726  0155061224            5\n",
       "2   276727  0446520802            0\n",
       "3   276729  052165615X            3\n",
       "4   276729  0521795028            6"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6eae7fe3",
   "metadata": {},
   "source": [
    "1.构建评分矩阵rating_matrix，将缺失值填充为 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "a24d179f",
   "metadata": {},
   "outputs": [],
   "source": [
    "#由考生填写\n",
    "rating_matrix = df.pivot_table(index='User-ID',columns='ISBN',values='Book-Rating')\n",
    "rating_matrix.fillna(0,inplace=True)\n",
    "#由考生填写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "6d79f319",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>ISBN</th>\n",
       "      <th>0002231115</th>\n",
       "      <th>0002232766</th>\n",
       "      <th>0002240114</th>\n",
       "      <th>000225669X</th>\n",
       "      <th>000254794</th>\n",
       "      <th>0006128831</th>\n",
       "      <th>0006379702</th>\n",
       "      <th>0006485294</th>\n",
       "      <th>000651118X</th>\n",
       "      <th>0006511929</th>\n",
       "      <th>...</th>\n",
       "      <th>9727474799</th>\n",
       "      <th>9728608225</th>\n",
       "      <th>9728609187</th>\n",
       "      <th>9782922145441</th>\n",
       "      <th>9871138016</th>\n",
       "      <th>9995585227</th>\n",
       "      <th>B0000BLD7X</th>\n",
       "      <th>B158991965</th>\n",
       "      <th>N3453124715</th>\n",
       "      <th>O6712345670</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>User-ID</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>276725</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>276726</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>276727</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>276729</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>276733</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>278407</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>278409</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>278411</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>278413</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>278418</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>679 rows × 4150 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "ISBN     0002231115  0002232766  0002240114  000225669X  000254794  \\\n",
       "User-ID                                                              \n",
       "276725          0.0         0.0         0.0         0.0        0.0   \n",
       "276726          0.0         0.0         0.0         0.0        0.0   \n",
       "276727          0.0         0.0         0.0         0.0        0.0   \n",
       "276729          0.0         0.0         0.0         0.0        0.0   \n",
       "276733          0.0         0.0         0.0         0.0        0.0   \n",
       "...             ...         ...         ...         ...        ...   \n",
       "278407          0.0         0.0         0.0         0.0        0.0   \n",
       "278409          0.0         0.0         0.0         0.0        0.0   \n",
       "278411          0.0         0.0         0.0         0.0        0.0   \n",
       "278413          0.0         0.0         0.0         0.0        0.0   \n",
       "278418          0.0         0.0         0.0         0.0        0.0   \n",
       "\n",
       "ISBN     0006128831  0006379702  0006485294  000651118X  0006511929  ...  \\\n",
       "User-ID                                                              ...   \n",
       "276725          0.0         0.0         0.0         0.0         0.0  ...   \n",
       "276726          0.0         0.0         0.0         0.0         0.0  ...   \n",
       "276727          0.0         0.0         0.0         0.0         0.0  ...   \n",
       "276729          0.0         0.0         0.0         0.0         0.0  ...   \n",
       "276733          0.0         0.0         0.0         0.0         0.0  ...   \n",
       "...             ...         ...         ...         ...         ...  ...   \n",
       "278407          0.0         0.0         0.0         0.0         0.0  ...   \n",
       "278409          0.0         0.0         0.0         0.0         0.0  ...   \n",
       "278411          0.0         0.0         0.0         0.0         0.0  ...   \n",
       "278413          0.0         0.0         0.0         0.0         0.0  ...   \n",
       "278418          0.0         0.0         0.0         0.0         0.0  ...   \n",
       "\n",
       "ISBN     9727474799  9728608225  9728609187  9782922145441  9871138016  \\\n",
       "User-ID                                                                  \n",
       "276725          0.0         0.0         0.0            0.0         0.0   \n",
       "276726          0.0         0.0         0.0            0.0         0.0   \n",
       "276727          0.0         0.0         0.0            0.0         0.0   \n",
       "276729          0.0         0.0         0.0            0.0         0.0   \n",
       "276733          0.0         0.0         0.0            0.0         0.0   \n",
       "...             ...         ...         ...            ...         ...   \n",
       "278407          0.0         0.0         0.0            0.0         0.0   \n",
       "278409          0.0         0.0         0.0            0.0         0.0   \n",
       "278411          0.0         0.0         0.0            0.0         0.0   \n",
       "278413          0.0         0.0         0.0            0.0         0.0   \n",
       "278418          0.0         0.0         0.0            0.0         0.0   \n",
       "\n",
       "ISBN     9995585227  B0000BLD7X  B158991965  N3453124715  O6712345670  \n",
       "User-ID                                                                \n",
       "276725          0.0         0.0         0.0          0.0          0.0  \n",
       "276726          0.0         0.0         0.0          0.0          0.0  \n",
       "276727          0.0         0.0         0.0          0.0          0.0  \n",
       "276729          0.0         0.0         0.0          0.0          0.0  \n",
       "276733          0.0         0.0         0.0          0.0          0.0  \n",
       "...             ...         ...         ...          ...          ...  \n",
       "278407          0.0         0.0         0.0          0.0          0.0  \n",
       "278409          0.0         0.0         0.0          0.0          0.0  \n",
       "278411          0.0         0.0         0.0          0.0          0.0  \n",
       "278413          0.0         0.0         0.0          0.0          0.0  \n",
       "278418          0.0         0.0         0.0          0.0          0.0  \n",
       "\n",
       "[679 rows x 4150 columns]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rating_matrix"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "39883b13",
   "metadata": {},
   "source": [
    "2.使用cosine_similarity函数计算项目相似度矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "bf6be954",
   "metadata": {},
   "outputs": [],
   "source": [
    "#  计算相似度矩阵\n",
    "#由考生填写\n",
    "item_similarity_matrix = cosine_similarity(rating_matrix.T)\n",
    "#由考生填写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "b17320ce",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0., 0., 0., ..., 0., 0., 0.],\n",
       "       [0., 0., 0., ..., 0., 0., 0.],\n",
       "       [0., 0., 1., ..., 0., 0., 0.],\n",
       "       ...,\n",
       "       [0., 0., 0., ..., 1., 0., 0.],\n",
       "       [0., 0., 0., ..., 0., 1., 0.],\n",
       "       [0., 0., 0., ..., 0., 0., 1.]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "item_similarity_matrix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "ba17483a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 将rating_matrix的索引转换为字符串\n",
    "rating_matrix.index=rating_matrix.index.map(str)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "f57b7f17",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>ISBN</th>\n",
       "      <th>0002231115</th>\n",
       "      <th>0002232766</th>\n",
       "      <th>0002240114</th>\n",
       "      <th>000225669X</th>\n",
       "      <th>000254794</th>\n",
       "      <th>0006128831</th>\n",
       "      <th>0006379702</th>\n",
       "      <th>0006485294</th>\n",
       "      <th>000651118X</th>\n",
       "      <th>0006511929</th>\n",
       "      <th>...</th>\n",
       "      <th>9727474799</th>\n",
       "      <th>9728608225</th>\n",
       "      <th>9728609187</th>\n",
       "      <th>9782922145441</th>\n",
       "      <th>9871138016</th>\n",
       "      <th>9995585227</th>\n",
       "      <th>B0000BLD7X</th>\n",
       "      <th>B158991965</th>\n",
       "      <th>N3453124715</th>\n",
       "      <th>O6712345670</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>User-ID</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>276725</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>276726</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>276727</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>276729</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>276733</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>278407</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>278409</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>278411</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>278413</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>278418</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>679 rows × 4150 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "ISBN     0002231115  0002232766  0002240114  000225669X  000254794  \\\n",
       "User-ID                                                              \n",
       "276725          0.0         0.0         0.0         0.0        0.0   \n",
       "276726          0.0         0.0         0.0         0.0        0.0   \n",
       "276727          0.0         0.0         0.0         0.0        0.0   \n",
       "276729          0.0         0.0         0.0         0.0        0.0   \n",
       "276733          0.0         0.0         0.0         0.0        0.0   \n",
       "...             ...         ...         ...         ...        ...   \n",
       "278407          0.0         0.0         0.0         0.0        0.0   \n",
       "278409          0.0         0.0         0.0         0.0        0.0   \n",
       "278411          0.0         0.0         0.0         0.0        0.0   \n",
       "278413          0.0         0.0         0.0         0.0        0.0   \n",
       "278418          0.0         0.0         0.0         0.0        0.0   \n",
       "\n",
       "ISBN     0006128831  0006379702  0006485294  000651118X  0006511929  ...  \\\n",
       "User-ID                                                              ...   \n",
       "276725          0.0         0.0         0.0         0.0         0.0  ...   \n",
       "276726          0.0         0.0         0.0         0.0         0.0  ...   \n",
       "276727          0.0         0.0         0.0         0.0         0.0  ...   \n",
       "276729          0.0         0.0         0.0         0.0         0.0  ...   \n",
       "276733          0.0         0.0         0.0         0.0         0.0  ...   \n",
       "...             ...         ...         ...         ...         ...  ...   \n",
       "278407          0.0         0.0         0.0         0.0         0.0  ...   \n",
       "278409          0.0         0.0         0.0         0.0         0.0  ...   \n",
       "278411          0.0         0.0         0.0         0.0         0.0  ...   \n",
       "278413          0.0         0.0         0.0         0.0         0.0  ...   \n",
       "278418          0.0         0.0         0.0         0.0         0.0  ...   \n",
       "\n",
       "ISBN     9727474799  9728608225  9728609187  9782922145441  9871138016  \\\n",
       "User-ID                                                                  \n",
       "276725          0.0         0.0         0.0            0.0         0.0   \n",
       "276726          0.0         0.0         0.0            0.0         0.0   \n",
       "276727          0.0         0.0         0.0            0.0         0.0   \n",
       "276729          0.0         0.0         0.0            0.0         0.0   \n",
       "276733          0.0         0.0         0.0            0.0         0.0   \n",
       "...             ...         ...         ...            ...         ...   \n",
       "278407          0.0         0.0         0.0            0.0         0.0   \n",
       "278409          0.0         0.0         0.0            0.0         0.0   \n",
       "278411          0.0         0.0         0.0            0.0         0.0   \n",
       "278413          0.0         0.0         0.0            0.0         0.0   \n",
       "278418          0.0         0.0         0.0            0.0         0.0   \n",
       "\n",
       "ISBN     9995585227  B0000BLD7X  B158991965  N3453124715  O6712345670  \n",
       "User-ID                                                                \n",
       "276725          0.0         0.0         0.0          0.0          0.0  \n",
       "276726          0.0         0.0         0.0          0.0          0.0  \n",
       "276727          0.0         0.0         0.0          0.0          0.0  \n",
       "276729          0.0         0.0         0.0          0.0          0.0  \n",
       "276733          0.0         0.0         0.0          0.0          0.0  \n",
       "...             ...         ...         ...          ...          ...  \n",
       "278407          0.0         0.0         0.0          0.0          0.0  \n",
       "278409          0.0         0.0         0.0          0.0          0.0  \n",
       "278411          0.0         0.0         0.0          0.0          0.0  \n",
       "278413          0.0         0.0         0.0          0.0          0.0  \n",
       "278418          0.0         0.0         0.0          0.0          0.0  \n",
       "\n",
       "[679 rows x 4150 columns]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rating_matrix"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8a8721de",
   "metadata": {},
   "source": [
    "3.在用户评分过的物品中获取与 item 相似的物品\n",
    "注意：for 下面的if else 是和for同层级的，如果写成上下级，结果显示为None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "65c976fc",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 定义预测用户对书籍的评分函数\n",
    "def predict_rating(user,item):\n",
    "    # 获取item的索引\n",
    "    item_index = rating_matrix.columns.get_loc(item)\n",
    "    # 获取item的相似度分数\n",
    "    similarity_score = item_similarity_matrix[item_index]\n",
    "    # 获取用户评分\n",
    "    user_ratings = rating_matrix.loc[user]\n",
    "    # 获取用户评分过的物品\n",
    "    rated_items = user_ratings[user_ratings>0].index\n",
    "    # 在用户评分过的物品中获取与item相似的物品\n",
    "    #由考生填写\n",
    "    similar_items = [item for item in rated_items if item_similarity_matrix[item_index][rating_matrix.columns.get_loc(item)]>0]\n",
    "    #由考生填写\n",
    "    similar_items_index = []\n",
    "    for i in range(len(similar_items)):\n",
    "        similar_items_index.append(rating_matrix.columns.get_loc(similar_items[i]))\n",
    "    # 如果没有相似的物品，则返回 0\n",
    "    if len(similar_items) == 0:\n",
    "        #print('len_similar_items == 0')\n",
    "        return 0\n",
    "    # 否则，计算用户对item的预测评分\n",
    "    else:\n",
    "        #print('len_similar_items != 0')\n",
    "        #由考生填写\n",
    "        return user_ratings[similar_items].dot(similarity_score[similar_items_index])/similarity_score[similar_items_index].sum()\n",
    "        #由考生填写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "c5872778",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "预测用户276729对书籍0064401367的评分为：0\n"
     ]
    }
   ],
   "source": [
    "#  测试\n",
    "user = '276729'\n",
    "item = '0064401367'\n",
    "predicted_rating1 = predict_rating(user,item)\n",
    "print('预测用户{}对书籍{}的评分为：{}'.format(user,item,predicted_rating1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "40859117",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "预测用户278259对书籍0020259700的评分为：10.0\n"
     ]
    }
   ],
   "source": [
    "# 测试，计算用户 278259 对书籍 0020259700 的预测评分\n",
    "user = '278259'\n",
    "item = '0020259700'\n",
    "predicted_rating2 = predict_rating(user,item)\n",
    "print('预测用户{}对书籍{}的评分为：{}'.format(user, item, predicted_rating2))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
